package com.yotoodesign.dianba.ui;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.app.GlobalApplication;

public class Share2Friends {

	private Context context;
	private PopupWindow popupWindow = null; 
	private View popupWindow_view ;
	private EditText etMsg;
	private LinearLayout bg;
	
	private OkClickListener onOkClickListener;
	private CancelClickListener onCancelClickListener;
	
	public Share2Friends(Context context) {
		this.context = context;
	}
	
	public Share2Friends show(View parent,int gravity){
		getPopupWindow(); 
		popupWindow.showAtLocation(parent, gravity, 0,0);
		AlphaAnimation aa = new AlphaAnimation(0,1);
		aa.setDuration(600);
		aa.setFillAfter(true);
		bg.setAnimation(aa);
		
		popupWindow.update();
		return this;
	}
	

	/*
	 * 获取PopupWindow实例
	 */
	private void getPopupWindow() {
		if (null != popupWindow) {
			popupWindow.dismiss();
			return;
		} else {
			initPopupWindow();
		}
	}

	/**
	 * 创建PopupWindow
	 */
	private void initPopupWindow() {
		// 得到屏幕的宽度和高度
		DisplayMetrics dm = new DisplayMetrics();
		GlobalApplication.getMainActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		// 获取自定义布局文件popupwindow.xml
		popupWindow_view = LayoutInflater.from(context).inflate(
				R.layout.share_friends, null);
		// 创建popupWindow实例
		popupWindow = new PopupWindow(popupWindow_view, screenWidth,
				screenHeight, true);
		// popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));
		bg = (LinearLayout)popupWindow_view.findViewById(R.id.bg);
		// 获取视图中的按钮与编辑框
		Button btnOk = (Button) popupWindow_view.findViewById(R.id.btnOk);
		Button btnCancel = (Button) popupWindow_view
				.findViewById(R.id.btnCancel);
		etMsg = (EditText) popupWindow_view.findViewById(R.id.etMsg);
		btnOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(onOkClickListener!=null){
					onOkClickListener.onClick(v);
				}
				popupWindow.dismiss();
			}
		});
		
		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(onCancelClickListener!=null){
					onCancelClickListener.onClick(v);
				}
				popupWindow.dismiss();
			}
		});
		
		
	}
	
	public interface OkClickListener{
		void onClick(View v);
	}
	
	public interface CancelClickListener{
		void onClick(View v);
	}

	public OkClickListener getOnOkClickListener() {
		return onOkClickListener;
	}

	public void setOnOkClickListener(OkClickListener onOkClickListener) {
		this.onOkClickListener = onOkClickListener;
	}

	public CancelClickListener getOnCancelClickListener() {
		return onCancelClickListener;
	}

	public void setOnCancelClickListener(CancelClickListener onCancelClickListener) {
		this.onCancelClickListener = onCancelClickListener;
	}

	public EditText getEtMsg() {
		return etMsg;
	}

	public void setEtMsg(EditText etMsg) {
		this.etMsg = etMsg;
	}

}
